package com.sie.controller;

import com.sie.dto.EmpFindPageDto;
import com.sie.dto.EmpUpdateDto;
import com.sie.dto.LoginNameDetectionDto;
import com.sie.dto.RoleDto;
import com.sie.service.EmpService;
import com.sie.service.RoleService;
import com.sie.service.impl.EmpServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 用户操作控制器
 */
@SuppressWarnings("all")
@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
@CrossOrigin(allowedHeaders = "*", origins = "*", allowCredentials = "true", methods = {RequestMethod.POST}, exposedHeaders = "*")
public class EmpController {
    @Autowired
    private EmpService empService;

    @Autowired
    private RoleService roleService;


    @ApiOperation("表格批量插入")
    @PostMapping("/upload")
    public Map upLoad(@RequestParam MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
        Map map = empService.addExcel(file, request);
        return map;
    }

    @ApiOperation("分页条件查询")
    @CrossOrigin
    @PostMapping("/findByPage")
    public Map findByPage(@ApiParam("entity") @RequestBody EmpFindPageDto dto, HttpServletRequest request) {
        return empService.findByPage(request, dto.getEntity(), dto.getBigTime(), dto.getSmallTime(), dto.getPageNum(), dto.getSize());
    }


    @ApiOperation("用户修改")
    @PostMapping("/update")
    public Map update(@RequestBody @ApiParam("修改信息实体类") EmpUpdateDto dto, HttpServletRequest request) {
        return empService.update(dto.getEntity(), dto.getPassword(), request);
    }

    @ApiOperation("用户名存在效验")
    @PostMapping("/loginNameDetection")
    private Map loginNameDetection(@ApiParam("loginName") @RequestBody LoginNameDetectionDto dto) {
        return empService.loginNameDetection(dto.getLoginName());
    }

    @ApiOperation("新增单个用户")
    @PostMapping("/addEmp")
    private Map addEmp(@RequestBody @ApiParam("接收员工信息") EmpUpdateDto dto, HttpServletRequest request) {
        return empService.addEmp(dto.getEntity(), dto.getPassword(), request);
    }

    @ApiOperation("根据id删除用户")
    @PostMapping("/delete")
    private Map deleteById(@ApiParam("接收员工信息") @RequestParam("id") String id, HttpServletRequest request) throws Exception {
        return empService.deleteById(id, request);
    }

    @ApiOperation("根据id数组批量删除")
    @PostMapping("/deleteList")
    private Map deletes(@RequestBody List<String> list, HttpServletRequest request) throws Exception {
        return empService.deleteList(list, request);
    }

    @ApiOperation("设置角色")
    @PostMapping("/setRole")
    public Map setRloe(@RequestBody RoleDto dto, HttpServletRequest request) {
        return roleService.setRole(request, dto.getId(), dto.getList());
    }
}
